<template>
  <el-dialog v-if="load" :close-on-click-modal="false" title="修改号码状态" v-model="visible" append-to-body width="500px" @close="handleClose">
    <el-radio-group v-model="form.phone_status" class="h-20">
      <el-radio v-for="(d, i) in options.phone_status" :key="i" :label="d.value">{{ d.label }}</el-radio>
    </el-radio-group>
    <template #footer>
      <el-button @click="close">关 闭</el-button>
      <el-button type="primary" :loading="loadings.submit" @click="submit">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from '../api'
import { ElMessage } from 'element-plus'

const props = defineProps({
  options: { type: Object, default: () => ({}) },
})

const load = ref(false)
const visible = ref(false)
let loadings = reactive({})
const form = ref({})

const emit = defineEmits(['close', 'success'])

// 打开弹窗
const open = async (selection) => {
  if (selection && !selection.length) {
    ElMessage.warning('请先选择行!')
    return
  }
  if (!load.value) {
    load.value = true
    await 1
  }
  loadings = reactive({})
  form.value.phone_status = 1
  form.value.phone_ids = selection
  visible.value = true
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const submit = () => {
  const {phone_status, phone_ids} = form.value
  loadings.submit = true
  api[phone_status === 1 ? 'enable' : 'disable']({phone_ids}).then(res => {
    ElMessage.success('操作成功')
    emit('success')
    close()
  }).finally(() => {
    loadings.submit = false
  })
}

defineExpose({ open })
</script>